package net.nandleon.AgenceVoyage.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.nandleon.AgenceVoyage.server.beans.*;

import org.hibernate.*;


public class DataBase {
	
	
	public static void addUser (String login, String passwd, int role)
	{
		Session session = HibernateUtil.currentSession();
		org.hibernate.Transaction tx = session.beginTransaction();

		User usr = new User();
		usr.setLogin(login);
		usr.setPasswd(passwd);
		usr.setRole(role);
		session.save(usr);
		   
		tx.commit();
		HibernateUtil.closeSession();
	}
	public static void changeUserPasswd (String login, String newPasswd)
	{
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();

		User usr =
			(User) session.load(User.class, login);

		usr.setPasswd(newPasswd);
		session.save(usr);

		tx.commit();
		HibernateUtil.closeSession();
	}
	public static User getUser (String login)
	{
		Session session = HibernateUtil.currentSession();
		User usr =
			(User) session.load(User.class, login);
		HibernateUtil.closeSession();
		return usr;
	}
	public static List<Concert> getConcert ()
	{
		List<Concert> concerts = new ArrayList<Concert>();
		Session session = HibernateUtil.currentSession();
		
		Query query = session.createQuery("from `nandleon`.`concert`"); 
		List list = query.list();
		Iterator it = list.iterator();
		while(it.hasNext())
		{
			concerts.add((Concert)it.next());
		}
		
		HibernateUtil.closeSession();
		return concerts;
	}
	public static List<Festival> getFestival ()
	{
		List<Festival> festivals = new ArrayList<Festival>();
		Session session = HibernateUtil.currentSession();
		
		Query query = session.createQuery("from `nandleon`.`festival`"); 
		List list = query.list();
		Iterator it = list.iterator();
		while(it.hasNext())
		{
			festivals.add((Festival)it.next());
		}
		
		HibernateUtil.closeSession();
		return festivals;
	}
	public static List<Monument> getMonuments ()
	{
		List<Monument> monuments = new ArrayList<Monument>();
		Session session = HibernateUtil.currentSession();
		
		Query query = session.createQuery("from `nandleon`.`musee`"); 
		List list = query.list();
		Iterator it = list.iterator();
		while(it.hasNext())
		{
			monuments.add((Monument)it.next());
		}
		
		HibernateUtil.closeSession();
		return monuments;
	}
	public static List<Musee> getMusees ()
	{
		List<Musee> musees = new ArrayList<Musee>();
		Session session = HibernateUtil.currentSession();
		
		Query query = session.createQuery("from `nandleon`.`musee`"); 
		List list = query.list();
		Iterator it = list.iterator();
		while(it.hasNext())
		{
			musees.add((Musee)it.next());
		}
		
		HibernateUtil.closeSession();
		return musees;
	}
	public static Offre getOffre (int numero)
	{
		Session session = HibernateUtil.currentSession();
		Query query = session.createQuery("from Offre where id_offre = :code ");
		query.setParameter("code", new Integer(numero).toString());
		List list = query.list();
		
		if (list.size() > 0)
			return (Offre)list.get(0);
		return null;
	}
	public static List<Offre> getOffres ()
	{
		List<Offre> offres = new ArrayList<Offre>();
		Session session = HibernateUtil.currentSession();
		
		Query query = session.createQuery("from `nandleon`.`musee`"); 
		List list = query.list();
		Iterator it = list.iterator();
		while(it.hasNext())
		{
			offres.add((Offre)it.next());
		}
		
		HibernateUtil.closeSession();
		return offres;
	}
	
}
